import React from "react";
import Map from "@biz/Map";
import BaseModal from "@biz/BaseModal";
import {LdhFormItem, LdhFormItemTip, LdhInput, LdhTextArea} from "../../../../biz/Components";

const prefix = "refund-modal";
export default class RefundModal extends BaseModal {

    constructor(props) {
        super(props);
        this.visibleStatus = "refund";
        this.title = "退款";
        this.className = prefix;
        this.width = 400;
        this.noOp= true;

    }

    componentWillReceiveProps(nextProps, nextContext) {
        let {modalStatus, detail} = nextProps.store;
        let oldModalStatus = this.props.store.modalStatus;
        if(modalStatus && modalStatus!=oldModalStatus){
            this.title = `退款 ${detail.orderNo} (${detail.totalAmount})`
        }else{
            this.title = `退款`
        }
    }

    onSubmit(values){
        let {action, store:{detail},onSubmitCallback} = this.props;
        let vo = {id: detail.id,...values}

        action.refund(vo, ()=>{
            this.onCancel();
            onSubmitCallback();
        });

    }
    onCancel(){
        let {action} = this.props;
        action.cancelModal();
    }

    renderDetail() {
        let {detail} = this.props.store;
        return <div>
            <LdhFormItem label="退款金额" name="refundAmount" required
                         rules={[
                             {pattern:window.CONFIG.REG_NUM_D2, message:"金额保留两位小数"},
                             {
                                 validator: (rule, newValue, callback) => {
                                     if (newValue != "" && newValue != undefined && Number(newValue) < 0) {
                                         return Promise.reject("退款金额要大于0");
                                     }
                                     if (newValue != "" && newValue != undefined && Number(newValue) > detail.totalAmount) {
                                         return Promise.reject(`退款金额要小于订单金额（${detail.totalAmount}）`);
                                     }
                                     return Promise.resolve();
                                 }
                             }
                             ]}>
                <LdhInput />
            </LdhFormItem>
            <LdhFormItem label="退款原因" name="refundReason" required>
                <LdhTextArea style={{ height: 80}} />
            </LdhFormItem>
        </div>
    }

}